package com.xiu.travel.portal;

import com.alibaba.fastjson2.JSON;
import com.xiu.travel.portal.result.ApiResult;
import com.xiu.travel.portal.client.LoginClient;
import com.xiu.travel.portal.result.TokenResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 授权
 * </p>
 *
 * @author YuLing
 * @since 2023-10-13
 */
@RestController
@RequestMapping("/portal/open/distribution/oauth")
@Tag(name = "鉴权接口")
@Slf4j
public class TravelLoginController {
    @Autowired
    private LoginClient loginClient;

    @GetMapping("token")
    @Operation(summary = "获取 access_token", description = "获取 access_token")
    public ApiResult token() {
        System.out.println("获取");
        ApiResult res = JSON.parseObject(loginClient.getAccessToken(OpenFeignConfig.CLIENT_ID, OpenFeignConfig.CLIENT_SECRET, OpenFeignConfig.GRANT_TYPE), ApiResult.class);
        OpenFeignConfig.TOKEN = res.data().toJavaObject(TokenResult.class).access_token();
        log.info("OpenFeignConfig.TOKEN:{}", OpenFeignConfig.TOKEN);
        return res;
    }

    @GetMapping("refresh")
    @Operation(summary = "刷新 access_token", description = "刷新 access_token")
    public ApiResult refresh() {
        System.out.println("access_token");
        ApiResult res = JSON.parseObject(loginClient.refreshAccessToken(OpenFeignConfig.CLIENT_ID, OpenFeignConfig.CLIENT_SECRET, OpenFeignConfig.TOKEN), ApiResult.class);
        log.info("OpenFeignConfig.TOKEN:{}", OpenFeignConfig.TOKEN);
        OpenFeignConfig.TOKEN = res.data().toJavaObject(TokenResult.class).access_token();
        log.info("OpenFeignConfig.TOKEN:{}", OpenFeignConfig.TOKEN);
        return res;
    }

}